package com.exartisan.molisasurvey;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockActivity;
import com.exartisan.bean.Data;

public class ResultActivity extends BaseActivity {

	private TextView tvUserName;
	private TextView tvEmail;
	private TextView tvPhonNumber;
	private TextView tvChidoan;
	private TextView tvDonvi;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
	}
	@Override
	protected void displayActivity() {
		setContentView(R.layout.activity_result);
	}

	@Override
	protected void initViews() {
		tvUserName = (TextView) findViewById(R.id.tvUserName);
		tvEmail = (TextView) findViewById(R.id.tvEmail);
		tvChidoan = (TextView) findViewById(R.id.tvChidoan);
		tvPhonNumber = (TextView) findViewById(R.id.tvPhoneNumber);
		tvDonvi = (TextView) findViewById(R.id.tvDonvi);
	}

	@Override
	protected void initVariables() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void initActions() {
		ArrayList<Data> datas = getUser().getData();
		for (Data data : datas) {
			Log.i("dt", data.getQuestionID() + " " + data.getUserAnswer());
		}
		Log.i("donvi", getUser().getDonvi());
		tvUserName.setText("Bạn: "+getUser().getUserName());
		tvEmail.setText("Email: " + getUser().getEmail());
		tvPhonNumber.setText("Sdt: " + getUser().getMobile());
		tvChidoan.setText("Chi đoàn: " + getUser().getChidoan());
		tvDonvi.setText("Đơn vị: " + getUser().getDonvi());
	}
	
	@Override
	public void onBackPressed() {
		confirmDialog();
	}
	
	public void onClickRestart(View view){
		confirmDialog();
	}

	private void confirmDialog(){
		AlertDialog.Builder restartAlertBuilder = new AlertDialog.Builder(this);
		restartAlertBuilder.setTitle("Thông báo");
		restartAlertBuilder
				.setMessage("Bạn muốn làm một phiếu khảo sát khác?");
		restartAlertBuilder.setCancelable(false);
		restartAlertBuilder.setPositiveButton("Có", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				getUser().setData(null);
				Intent i = new Intent(getBaseContext(),
						InputInfoActivity.class);
				i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(i);
			}
		});
		restartAlertBuilder.setNegativeButton("Không", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				getUser().setData(null);
				for (SherlockActivity e : ActivityHolder.allActivities) {
					e.finish();
				}
				Intent intent = new Intent(Intent.ACTION_MAIN);
				intent.addCategory(Intent.CATEGORY_HOME);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(intent);
			}
		});

		AlertDialog restartAlertDialog = restartAlertBuilder.create();
		restartAlertDialog.show();
	}
}
